﻿using HongStone.Domain.ContractModel;
using HongStone.Ince;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace HongStone.API.Controllers
{
    [ApiExplorerSettings(GroupName = "contract")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class contractController : ControllerBase
    {
        private readonly IContractInce _ince;
        public contractController(IContractInce ince)
        {
            _ince = ince;
        }
        /// <summary>
        /// 合同数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetThecontracts(string ContractName, string Constructionunit, string Originalamount, int ContractclassificationId,int pageindex=1, int pagesize=2)
        {
            var totalcount = 0;
            var list = _ince.GetThecontracts(out totalcount, pageindex, pagesize, ContractName, Constructionunit, Originalamount, ContractclassificationId);
            return Ok(new { totalcount, list });
        }
        /// <summary>
        /// 字典数据
        /// </summary>
        /// <returns></returns>
       [HttpGet]
        public IActionResult GetZiDian()
        {
            return Ok(_ince.GetZiDian());
        }
        /// <summary>
        /// 建筑分类
        /// </summary>
        /// <returns></returns>
       [HttpGet]
        public IActionResult GetarcClass()
        {
            return Ok(_ince.GetarcClass());
        }

        /// <summary>
        /// 结构形式
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStrstyle()
        {
            return Ok(_ince.GetStrstyle());
        }
        /// <summary>
        /// 合同信息添加
        /// </summary>
        /// <param name="thecontract"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddContract(Thecontract thecontract)
        {
            return Ok(_ince.AddContract(thecontract));
        }
        /// <summary>
        /// 工程负责人信息
        /// </summary>
        /// <returns></returns>
       [HttpGet]
        public IActionResult GetProjectLeaders()
        {
            return Ok(_ince.GetProjectLeaders());
        }
        /// <summary>
        /// 收费列表详细
        /// </summary>
        /// <returns></returns>
       [HttpGet]
        public IActionResult GetCharges()
        {
            return Ok(_ince.GetCharges());
        }
        /// <summary>
        /// 收费列表信息添加
        /// </summary>
        /// <param name="charge"></param>
        /// <returns></returns>
       [HttpPost]
        public IActionResult AddCharges(charge charge)
        {
            return Ok(_ince.AddCharges(charge));
        }
        /// <summary>
        /// 收费列表删除
        /// </summary>
        /// <param name="Cid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelCharges(int Cid)
        {
            return Ok(_ince.DelCharges(Cid)); 
        }
        /// <summary>
        /// 反填客户数据
        /// </summary>
        /// <param name="CustomerId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCustomer(int CustomerId)
        {
            return Ok(_ince.GetCustomer(CustomerId));
        }
        /// <summary>
        /// 客户信息显示
        /// </summary>
        /// <returns></returns>
       [HttpGet]
        public IActionResult GetCustomers()
        {
            return Ok(_ince.GetCustomers());
        }
        /// <summary>
        /// 合同数据批量删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
       [HttpDelete]
        public IActionResult DelAllThecont(string Ids)
        {
            return Ok(_ince.DelAllThecont(Ids));
        }
        /// <summary>
        /// 合同数据反填
        /// </summary>
        /// <param name="ContractId"></param>
        /// <returns></returns>
       [HttpGet]
        public IActionResult EditThecontract(int ContractId)
        {
            return Ok(_ince.EditThecontract(ContractId));
        }
        /// <summary>
        /// 导出Excel表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownLoad()
        {
            var aa=0;
            var valuest = _ince.GetThecontracts( out aa, 1, 10000, "", "", "", 1);
            var values = new List<Dictionary<string, string>>();
            foreach (var item in valuest)
            {
                values.Add(new Dictionary<string, string>
                {
                    { "合同序号",$"{item.ContractNo}"},{ "文本编号",$"{item.ContractextNo}"},{ "工程名称",$"{item.ContractName}"},{ "合同分类",$"{item.ContractclassificationId}"},{ "建筑规模(㎡)",$"{item.Builtuparea}"},{ "原始合同额",$"{item.Originalamount}"},{ "实际合同额",$"{item.Actualamount}"},{ "子公司",$"{item.SubsidiaryId}"},{ "签约日期",$"{item.Contractsigningdate}"},{ "工程负责人",$"{item.ProjectLeaderId}"},{ "建设单位",$"{item.Constructionunit}"},{ "甲方类型",$"{item.FirstpartyId}"},{ "结构形式",$"{item.structuralstyleId}"},{ "建筑分类",$"{item.ArchitectureId}"},{ "工程负责人电话",$"{item.TelephoneB}"},{ "甲方负责人",$"{item.PersonA}"},{ "甲方负责人电话",$"{item.TelephoneA}"},{ "工程地点",$"{item.Projectlocation}"},{ "合同备注",$"{item.Contractremarks}"}
                });
            }
            var memorystream = new MemoryStream();
            memorystream.SaveAs(values);
            memorystream.Seek(00, SeekOrigin.Begin);
            return new FileStreamResult(memorystream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "demo.xlsx"
            };
        }
        /// <summary>
        /// 项目信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetProjects()
        {
            return Ok(_ince.GetProjects());
        }
        /// <summary>
        /// 收费数据反填
        /// </summary>
        /// <param name="CId"></param>
        /// <returns></returns>
       [HttpGet]
        public IActionResult Editcharge(int sid)
        {
            return Ok(_ince.Editcharge(sid));
        }
        /// <summary>
        /// 收费数据修改
        /// </summary>
        /// <param name="charge"></param>
        /// <returns></returns>
       [HttpPut]
        public IActionResult Updatecharge(charge charge)
        {
            return Ok(_ince.Updatecharge(charge));
        }
        /// <summary>
        /// 查询合同金额信息
        /// </summary>
        /// <param name="Thecount"></param>
        /// <param name="fkcount"></param>
        /// <param name="wfcount"></param>
        /// <param name="jscount"></param>
        /// <param name="ze"></param>
        /// <param name="sk"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Getjine()
        {
            return Ok(_ince.Getjine());
        }
        [HttpGet]
        public IActionResult Getjidu()
        {
            return Ok(_ince.Getjidu());
        }
        /// <summary>
        /// 按月份查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAnyues()
        {
            return Ok(_ince.GetAnyues());
        }
    }
}
